﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WaterKit.DbBase;
using System.Data;
using WaterKit.Base;

namespace Auction_Server.Business
{
    /// <summary>
    /// 标的服务
    /// </summary>
    public class targetService : OracleHelper
    {
        /// <summary>
        /// 根据拍卖会编号获取标的列表
        /// </summary>
        /// <param name="auction_no">拍卖会编号</param>
        /// <returns>DataTable</returns>
        public DataTable GetTargetList(string auction_no)
        {
            StringBuilder sql = new StringBuilder();
            try
            {
                sql.Append(" SELECT t1.TARGET_NO,t1.TARGET_NAME,t1.BID_TYPE,");
                sql.Append(" t1.NOTICE,t1.AUCTION_TYPE,t1.INCREMENT_MONEY,");
                sql.Append(" t1.DEPOSIT,t1.BIDUSER,t1.BIDPRICE ,t1.STATUS ,");
                sql.Append(" t1.FIX_TIME,t1.LIMIT_TIME,t1.BID_TIME, ");
                //LOW_PRICE:商品的拍卖底价
                sql.Append(" t2.PRODUCT_NO,t2.PRODUCT_NAME,t2.PRODUCT_LV,t2.PRICE AS LOW_PROCE, ");
                sql.Append(" t2.MEASUREMENT,t2.AUCTION_METE,t2.DESCRIPTION, t2.PRODUCT_TYPE,");
                sql.Append(" t3.WAREHOUSE_NO,t3.WAREHOUSE_NAME,t3.WAREHOUSE_ADD,  ");
                sql.Append(" t4.ATTACHMENT_NAME as product_PIC_Name,t4.ATTACHMENT_ID AS product_ID,t4.ATTACHMENT_PATH AS product_PIC, ");
                sql.Append(" t5.UNIT_NO,t5.UNIT_NAME, t5.UNIT_ADD,");

                sql.Append(" t0.AUCTION_NO,t0.AUCTION_NAME, t0.AUCTION_DATE, t0.AUCTION_TIME,");
                sql.Append(" t0.AUCTION_ADD,t0.AUCTION_STATUS, t0.AUCTION_MEMO,");
                sql.Append(" t6.AUCTIONEER_CODE,t6.AUCTIONEER_NAME, t6.CERTIFICATE_NUMBER ");

                sql.Append(" FROM auction t0 ,target t1, product t2,warehouse t3,");
                sql.Append(" attachment t4,tb_unit t5,auctioneer t6 ");

                sql.Append(" WHERE  t1.del_flg=0 and t1.auction_no = '").Append(auction_no).Append("' ");
                sql.Append(" and t1.auction_no = t0.auction_no and t0.del_flg=0 ");
                sql.Append(" and t1.auctioneer_code = t6.auctioneer_code(+) ");
                sql.Append(" and t1.product_no = t2.product_no and t2.del_flg=0  ");
                sql.Append(" and t2.storage_id =t3.warehouse_id(+) and t2.attachment_id = t4.attachment_id(+) ");
                sql.Append(" and t2.unit_no = t5.unit_no(+)   ");


                return RetriveDataTable(sql.ToString());
            }
            catch (Exception ex)
            {
                Log4C.WriteErrLog("获取指定拍卖会下的标的列表失败", ex);
                return null;
            }
        }

        /// <summary>
        /// 根据标的编号获取参与竞拍单位信息
        /// </summary>
        /// <param name="target_no">标的编号</param>
        /// <returns>DataTable</returns>
        public DataTable getTargetCompany(string target_no)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append(" SELECT t1.BIDUSER_NO,t1.COMPANY_NO,t1.COMPANY_NAME, ");
            sql.Append(" t2.SEATNUMBER,t2.TARGET_NO,MARGIN_STATUS,AUCTION_USERNAME");
            sql.Append(" FROM BIDUSER t1,AUCTION_USER t2 ");
            sql.Append(" WHERE  t1.BIDUSER_NO=t2.BIDUSER_NO");
            sql.Append(" AND t2.TARGET_NO= '").Append(target_no).Append("'");

            return RetriveDataTable(sql.ToString());
        }

        /// <summary>
        /// 更新标的信息
        /// </summary>
        /// <param name="target_no">标的号</param>
        /// <param name="bid_user">中标用户</param>
        /// <param name="bid_price">中标价</param>
        /// <param name="status">状态</param>
        /// <returns>布尔</returns>
        public bool setTarget(string target_no, string bid_user, string bid_price, string status)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("UPDATE TARGET SET STATUS='");
            sql.Append(status).Append("'");
            sql.Append(",BIDUSER='").Append(bid_user).Append("'");
            sql.Append(",BIDPRICE='").Append(bid_price).Append("'");
            sql.Append(" WHERE TARGET_NO='").Append(target_no).Append("'");

            return queryNonSQL(sql.ToString());
        }

        /// <summary>
        /// 更新商品状态
        /// </summary>
        /// <param name="product_no">商品编号</param>
        /// <param name="status">状态</param>
        /// <returns>布尔</returns>
        public bool setTarget(string product_no, string status)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("UPDATE product SET STATUS='");
            sql.Append(status).Append("'");
            sql.Append(" WHERE PRODUCT_NO='").Append(product_no).Append("'");

            return queryNonSQL(sql.ToString());
        }

        /// <summary>
        /// 获取拍卖会下待拍标的数量
        /// </summary>
        /// <param name="auction_no">拍卖会编号</param>
        /// <param name="status">状态</param>
        /// <returns>int</returns>
        public int getTargetCount(string auction_no, string status)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append(" SELECT count(*) FROM target where AUCTION_NO='");
            sql.Append(auction_no).Append("' AND");
            sql.Append(" STATUS='").Append(status).Append("'");

            return Convert.ToInt32(ExecuteScalar(sql.ToString()));
        }
    }
}
